package avicit.bdp.dgs.qa.rest;

import avicit.bdp.dgs.qa.dto.AnalysisDTO;
import avicit.bdp.dgs.qa.service.AnalysisService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-09-07
 * @类说明：AnalysisRest
 * @修改记录：
 * @注意事项：
 * @主要功能：质量分析Rest接口
 */
@RestController
@Api(tags = "qa", description = "质量分析Rest接口")
@RequestMapping("/api/bdp/dgs/qa/analysis")
public class AnalysisRest {
    @Autowired
    private AnalysisService analysisService;

    /**
     * 新增质量分析
     *
     * @param name
     * @param selectedTime
     * @param type
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增质量分析")
    public ResponseMsg<String> add(@ApiParam(value = "name", name = "name") @RequestParam String name,
                                   @ApiParam(value = "selectedTime", name = "selectedTime") @RequestParam String selectedTime,
                                   @ApiParam(value = "type", name = "type") @RequestParam Integer type,
                                   @ApiParam(value = "secretLevel", name = "secretLevel") @RequestParam String secretLevel) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.analysisService.addAnalysis(name, selectedTime, type, secretLevel));

        return responseMsg;
    }

    /**
     * 删除/批量删除质量分析，多个id使用英文逗号隔开
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除/批量删除质量分析，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> delete(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.analysisService.deleteAnalysis(id));

        return responseMsg;
    }

    /**
     * 查询质量分析详情
     *
     * @param id 质量作业ID
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "查询质量分析详情")
    public ResponseMsg<AnalysisDTO> detail(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<AnalysisDTO> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.analysisService.getAnalysisDetail(id));

        return responseMsg;
    }

    /**
     * 分页查询质量分析
     *
     * @param keyWords
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "分页查询质量分析")
    public ResponseMsg<QueryRespBean<AnalysisDTO>> query(
            @ApiParam(value = "质量分析类型", name = "type") @RequestParam Integer type,
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<AnalysisDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.analysisService.queryAnalysis(type, keyWords, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 判读名称是否重复
     *
     * @param name
     * @return true:重复；false:不重复
     */
    @GetMapping("/isSameName")
    @ApiOperation(value = "判读质量作业名称是否重复")
    public ResponseMsg<Boolean> isSameName(@ApiParam(value = "名字", name = "name") @RequestParam String name) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.analysisService.isSameName(name));

        return responseMsg;
    }

    /**
     * 下载报告
     *
     * @param id 日报/月报ID
     * @return
     */
    @GetMapping("/download")
    @ApiOperation(value = "下载质量报告")
    public void downloadReport(@ApiParam(value = "质量作业ID", name = "jobId") @RequestParam(required = false) String id,
                               HttpServletResponse response) {
        this.analysisService.downloadReport(id, response);
    }
}
